﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ABCTile.Models;
using ABCTile.DAL;
using ABCTile.ViewModel;
using PagedList;

namespace ABCTile.Controllers
{
    public class CompanyController : Controller
    {
        private ABCDbContext db = new ABCDbContext();

        //
        // GET: /Tile/
        [Authorize]
        public ViewResult Index(int? page, CompanyIndexData CompanyIndexData)
        {
            var ViewModel = new CompanyIndexData();

            ViewModel.Companies = db.Company.ToList();

            if (Request.HttpMethod != "GET")
            {
                page = 1;
            }

            int pageSize = 10; //set page size, 10 tile data will be displayed on the screen
            int pageIndex = (page ?? 1) - 0; //set page index

            ViewModel.PagedCompanies = ViewModel.Companies.ToPagedList(pageIndex, pageSize); //Convert "viewModel.tiles" into "PagedList" Type to diplay 10 tile data on a page

            return View(ViewModel);

        }

        [Authorize]
        public ViewResult Search(string companyName, string streetAddr, string officePhone)
        {
            // Search Logic ********************************************************
            var companies = from c in db.Company
                            select c;

            if (!String.IsNullOrEmpty(companyName))
            {
                companies = companies.Where(s => s.Company_Name.Contains(companyName));
            }
            if (!String.IsNullOrEmpty(streetAddr))
            {
                companies = companies.Where(s => s.Street_Address.Contains(streetAddr));
            }
            if (!String.IsNullOrEmpty(officePhone))
            {
                companies = companies.Where(s => s.Office_Phone.Contains(officePhone));
            }

            return View(companies);
        }

        //
        // GET: /Tile/Details/5
        [Authorize]
        public ViewResult Details(int id)
        {
            Company company = db.Company.Find(id);

            if (company == null)
            {
                return View();
            }
            return View(company);
        }


        //
        // GET: /Tile/Create
        [Authorize]
        public ViewResult Create()
        {
            return View();
        }

        //
        // POST: /Tile/Create

        [HttpPost]
        public ActionResult Create(Company company)
        {
            if (ModelState.IsValid)
            {
                var companies = from c in db.Company
                                select c;
                foreach (Company c in companies)
                {
                    if (company.Branch_Seq == c.Branch_Seq && company.Company_Name == c.Company_Name)
                    {
                        ViewBag.Error = "The company and branch you entered are already present in the database. Please search for the correct company.";
                        return View(company);
                    }
                    else
                    {
                        ViewBag.Error = "";
                        db.Company.Add(company);
                        db.SaveChanges();

                        return RedirectToAction("Index");
                    }
                }
            }

            return View(company);
        }

        //
        // GET: /Tile/Edit/5
        [Authorize]
        public ViewResult Edit(int id)
        {
            Company company = db.Company.Find(id);

            return View(company);
        }

        //
        // POST: /Tile/Edit/5

        [HttpPost]
        public ActionResult Edit(Company company)
        {
            if (ModelState.IsValid)
            {
                db.Entry(company).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(company);
        }

        //
        // GET: /Tile/Delete/5
        [Authorize]
        public ViewResult Delete(int id)
        {
            Company company = db.Company.Find(id);
            return View(company);
        }

        //
        // POST: /Tile/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Company company = db.Company.Find(id);
            db.Company.Remove(company);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);

        }

    }
}